package com.car.service.reception.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import com.car.dal.dao.client.TgClientDao;
import com.car.dal.dao.reception.TgReceptionDao;
import com.car.dal.dao.sudent.StudentDao;
import com.car.dal.object.StuMsg;
import com.car.dal.object.TgClient;
import com.car.dal.object.TgReception;
import com.car.dal.object.TgStudent;
import com.car.service.reception.ReceptionService;
import com.car.util.JsonValidator;
import com.car.util.StringUtil;

public class ReceptionServiceImpl implements ReceptionService {
	
	@Resource
	private TgReceptionDao receptionDao;
	@Resource
	private TgClientDao clientDao;
	@Resource
	private StudentDao studentDao;

	//咨询师总接待量，排除市场
	@Override
	public Integer getZonlReceptionCount(TgReception reception) {
		Integer zonRece = 0;
		List<TgReception> jdlist = receptionDao.getZonlReceptionCount(reception);
        List<String> sList = new ArrayList<String>();
        if(jdlist!=null && jdlist.size()>0){
        	for(TgReception r:jdlist){
        		 //学生信息
                TgClient client = new TgClient();
                client.setKeyname(r.getClient());
                List<TgStudent> stuList = null;

                List<TgClient> cliList = clientDao.findByExample(client);
                if (cliList != null && cliList.size() > 0) {
                    TgStudent stu1 = new TgStudent();
                    stu1.setPkey(cliList.get(0).getKeyname());
                    stuList = studentDao.findByExample(stu1);

                    String student = r.getStudent();
                    if (student != null && !"".equals(student)) {
                        try {
                            JsonValidator json = new JsonValidator();
                            Boolean flag = json.validate(student);
                            String ObjContent = "";
                            if (flag) {
                                ObjContent = student;
                            } else {
                                byte[] bytes = StringUtil.hexDecode(student);
                                ObjContent = new String(bytes, "UTF-8");
                            }
                            if (ObjContent.contains("key")) {
                                JSONObject obj = JSONObject.fromObject(ObjContent);
                                
                                if (stuList != null && stuList.size() > 0) {
                                    for (TgStudent s : stuList) {
                                        JSONObject obj2 = null;
                                        if (ObjContent.contains("\"key\":\"" + s.getKeyname() + "\"")) {
                                            obj2 = obj.getJSONObject(s.getKeyname());
                                        }
                                        if (obj2 != null) {
                                            StuMsg stuMsg = new StuMsg();
                                            stuMsg.setKeyname(obj2.getString("key"));
                                            stuMsg.setMoneylog(obj2.getString("moneylog"));
                                            stuMsg.setPrice(obj2.getString("price"));
                                            stuMsg.setReason(obj2.getString("reason"));
                                            stuMsg.setResult(obj2.getString("result"));
                                            stuMsg.setTestuser(s.getNickname());
                                            sList.add(obj2.getString("key"));
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
        	}
            if(sList!=null && sList.size()>0){
            	zonRece = StringUtil.getStringListQccf(sList).size();
            }
        }
		return zonRece;
	}
}
